package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JcMyFactoryListEntity;
import com.sgb.jicai.view.activity.FactoryJicaiActivity;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.JcMyFactoryDataEntity;
import com.sgb.jicai.entity.JcCountEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FactoryJiCaiModel extends ViewModel {

    public boolean hasNext = false;//是否有下一页
    public void getList(FactoryJicaiActivity activity, int status, int page, String searchKey) {
        Map map = new HashMap();
        map.put("limit", 10);
        map.put("page", page);
        map.put("status", status == 0 ? "" : status);
        map.put("goodsName", searchKey);
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
       JiCaiBaseApi.getApi().getMyFactoryCenFaQiList(map).enqueue(new ICallBack<BaseEntity<JcMyFactoryDataEntity<JcMyFactoryListEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<JcMyFactoryDataEntity<JcMyFactoryListEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null)
                    return;
                if (null == data.getData())
                    return;
                if (data.getData().getCurrent() < data.getData().getPages()) {
                    hasNext = true;
                } else {
                    hasNext = false;
                }
                activity.showList(data.getData().getRecords(), hasNext);
            }
        });
    }

    public void getH5Url(FactoryJicaiActivity activity,JcMyFactoryListEntity entity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting("Factorydetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.showUrl(data.getData().getUrl() + "?id=" + entity.getNo());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * //刷新数量
     */
    public void getMyOfferBuyListCount(FactoryJicaiActivity activity,String searchWords) {
//        Map map = new HashMap();
//        map.put("type", 1);//搜索类型：1，自己发起的集采。2，加盟的集采
//        map.put("goodsName", searchWords);
        JiCaiBaseApi.getApi().getMyProductListCountForStatus(searchWords,1).enqueue(new ICallBack<BaseEntity<List<JcCountEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JcCountEntity>> data) {
                if (data.getData() == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.setTabNum(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    //停止集采
    public void stopJiCai(FactoryJicaiActivity activity,JcMyFactoryListEntity entity) {
        JiCaiBaseApi.getApi().stopFactoryJc(entity.getNo()).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(activity, "停止集采成功");
                    activity.getData();
                } else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }

    //删除供方发起订单
    public void deleteFactory(FactoryJicaiActivity activity,String no) {
        DialogHelper.showProgressDialog(activity, null, "删除数据中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().productSaleRecordUCenterDelete(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(activity, "删除信息成功");
                    activity.initData();
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });

    }
}
